#include "vertex_types.fxh"
#include "Samplers.fxh"
#include "Functions.fxh"

float4 RenderNoiseAndVignetingPS(float2 UV:TEXCOORD0) : COLOR
{
	float4 staged_texture=tex2D(S0LinearSampler, UV)*tex2D(S1LinearSampler, UV);
	return staged_texture;
}


technique tecNoiseAndVigneting
{
	pass p0 
	{
		PixelShader = compile ps_3_0 RenderNoiseAndVignetingPS();
	}
}

//*****************************************************************************
float g_BlurWeight = 1;
float PixelKernel[13] =
{
    -6,
    -5,
    -4,
    -3,
    -2,
    -1,
     0,
     1,
     2,
     3,
     4,
     5,
     6,
};

static const float BlurWeights[13] = 
{
    0.002216,
    0.008764,
    0.026995,
    0.064759,
    0.120985,
    0.176033,
    0.199471,
    0.176033,
    0.120985,
    0.064759,
    0.026995,
    0.008764,
    0.002216,
};

// Effect function
float4 RenderGaussianBlurPS( float2 Tex : TEXCOORD0 ) : COLOR
{
    // Apply surrounding pixels
    float4 color = 0;
    float2 samp = Tex;
    samp.y = Tex.y;

    for (int i = 0; i < 13; i++) 
	{
        samp.x = Tex.x + PixelKernel[i] * (1/g_ScreenWidth);
        color += tex2D(S0LinearSampler, samp.xy) * BlurWeights[i];
    }
	color = color*g_BlurWeight + tex2D(S0LinearSampler, Tex.xy) * (1-g_BlurWeight);
    return color;
}

technique tecGaussianBlur
{
	pass p0 
	{
		PixelShader = compile ps_3_0 RenderGaussianBlurPS();
	}
}
//************************************************************************

float4 RenderDeferredZBlurPS(float2 Tex: TEXCOORD0) : COLOR
{
	
	float l_FocalStart=10;
	float l_FocalEnd=20;
	float l_ZBlurEnd=30;
	float l_BlurConst=0.3;
	
	//Cálculo del Gaussian Blur
	float4 l_DiffuseColor=tex2D(S0LinearSampler, Tex);
	
	float l_Depth=tex2D(S1LinearSampler, Tex);
	float3 l_WorldPosition=GetPositionFromZDepthView(l_Depth, Tex, g_InverseViewMatrix, g_InverseProjectionMatrix);
	
	
	
	// Apply surrounding pixels
    float4 color = 0;
    float2 samp = Tex;
    samp.y = Tex.y;

    for (int i = 0; i < 13; i++) {
        samp.x = Tex.x + PixelKernel[i] * (1/g_ScreenWidth);
        color += tex2D(S0LinearSampler, samp.xy) * BlurWeights[i];
    }
	//Fin del cálculo del gaussian Blur
	
	
	float l_Dist = length(l_WorldPosition- g_CameraPosition);
	int l_nearBlur= saturate(l_FocalStart/l_Dist);
	
	g_BlurWeight=(float(l_nearBlur)*l_BlurConst)+(saturate((l_Dist-l_FocalEnd)/(l_ZBlurEnd-l_FocalEnd)));
	
	color = color*g_BlurWeight + tex2D(S0LinearSampler, Tex.xy) * (1-g_BlurWeight);
	
	return color;
}

technique tecDeferredZBlur
{
	pass p0
	{
		AlphaBlendEnable = false;
		VertexShader = null;
		PixelShader = compile ps_3_0 RenderDeferredZBlurPS();
	}
}
//**************************************************************************
float4 g_FogColor = (1.0, 0.0, 0.0, 1.0);
float g_StartLinearFog =  40;
float g_EndLinearFog =  90;
float g_atMax =  70;

float CalcAttenuation(float Depth, float StartFog, float EndFog)
{
	return  (EndFog - Depth) / (EndFog - StartFog);
}

float4 CalcLinearFog(float Depth, float StartFog, float EndFog, float4 FogColor)
{
	if(Depth<g_atMax){
		return float4(FogColor.xyz, FogColor.a*(1.0-CalcAttenuation(Depth, StartFog, EndFog)));	
	}
	else{
		return float4(FogColor.xyz, FogColor.a*(1.0-CalcAttenuation(g_atMax, StartFog, EndFog)));	
	}
}

float4 RenderQuadFogPS(in float2 UV : TEXCOORD0) : COLOR
{
	float4 l_DepthColor=tex2D(S0LinearSampler, UV);
	//float3 l_ViewPosition=GetPositionFromZDepthViewInViewCoordinates(l_DepthColor, UV, g_InverseProjectionMatrix);
	float3 l_WorldPosition=GetPositionFromZDepthView(l_DepthColor, UV, g_InverseViewMatrix, g_InverseProjectionMatrix);
	float l_Depth=length(g_CameraPosition-l_WorldPosition);
	//return CalcExp2Fog(l_Depth, g_Exp2DensityFog, g_FogColor);
	//return CalcExpFog(l_Depth, g_ExpDensityFog, g_FogColor);
	return CalcLinearFog(l_Depth, g_StartLinearFog, g_EndLinearFog, g_FogColor);
}

technique DrawQuadFogTechnique
	{
	pass p0
	{
		AlphaBlendEnable = true;
		BlendOp=Add;
		SrcBlend=SrcAlpha;
		DestBlend=InvSrcAlpha;
		PixelShader=compile ps_3_0 RenderQuadFogPS();
	}
}
